//
//  JMAcademyCell.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/5.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMAcademyCell.h"
#import <UIImageView+WebCache.h>
#import "JMHeader.h"
#import "UIViewExt.h"
#import "NSString+date.h"


@interface JMAcademyCell ()
{
    NSArray *dataArray;
}

@property (nonatomic, strong) UILabel *acceptanceRateLabel2;

@property (nonatomic, strong) NSMutableArray *labelArray;

@property (nonatomic, strong) UIImageView *bottomLineImageView;
@end

@implementation JMAcademyCell

- (UIImageView *)bottomLineImageView
{
    if (!_bottomLineImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"projectImage.bundle/line_image"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor clearColor];
        _bottomLineImageView = imageView;
        [self addSubview:_bottomLineImageView];
    }
    return _bottomLineImageView;
}

- (NSMutableArray *)labelArray
{
    if (!_labelArray) {
        _labelArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _labelArray;
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@""];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.masksToBounds = YES;
        imageView.layer.borderColor = [UIColorFromRGB(0xc4c4c4) CGColor];
        imageView.layer.borderWidth = 0.5;
        imageView.layer.cornerRadius = 8;
        _iconImageView = imageView;
        [self addSubview:_iconImageView];
    }
    return _iconImageView;
}

- (UILabel *)titleLable
{
    if (!_titleLable) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.046875];
        [self addSubview:_titleLable = label];
    }
    return _titleLable;
}

- (UILabel *)acceptanceRateLabel
{
    if (!_acceptanceRateLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = UIColorFromRGB(0xeb6100);
        label.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125];
        _acceptanceRateLabel = label;
        [self addSubview:_acceptanceRateLabel];
    }
    return _acceptanceRateLabel;
}

- (UILabel *)acceptanceRateLabel2
{
    if (!_acceptanceRateLabel2) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = UIColorFromRGB(0x797979);
        label.text =@"录取率:";
        label.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125];
        _acceptanceRateLabel2 = label;
        [self addSubview:_acceptanceRateLabel2];
    }
    return _acceptanceRateLabel2;
}

- (JMAttenButton *)attentionBtn
{
    if (!_attentionBtn) {
        JMAttenButton *button = [JMAttenButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:UIColorFromRGB(0xa6a6a6) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        _attentionBtn = button;
        _attentionBtn.backgroundColor = [UIColor clearColor];
        [self addSubview:_attentionBtn];
    }
    return _attentionBtn;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.iconImageView.frame = CGRectMake(10, (self.frameHeight - UI_SCREEN_WITH *0.184375)/2.0, UI_SCREEN_WITH *0.184375, UI_SCREEN_WITH *0.184375);
    self.titleLable.frame = CGRectMake(self.iconImageView.frameRight + 15, self.iconImageView.frameTop, UI_SCREEN_WITH * 0.3125, UI_SCREEN_WITH *0.0625);
    self.acceptanceRateLabel2.frame = CGRectMake(self.titleLable.frameLeft, self.iconImageView.center.y - UI_SCREEN_WITH *0.0625/2.0, UI_SCREEN_WITH *0.115375, UI_SCREEN_WITH *0.0625);
    self.acceptanceRateLabel.frame = CGRectMake(self.acceptanceRateLabel2.frameRight, self.acceptanceRateLabel2.frameTop,120, self.acceptanceRateLabel2.frameHeight);
    self.attentionBtn.frame = CGRectMake(UI_SCREEN_WITH - UI_SCREEN_WITH *0.119375 , 5, UI_SCREEN_WITH *0.119375, 20);
   
    self.attentionBtn.imageEdgeInsets = UIEdgeInsetsMake(0,-self.attentionBtn.frameWidth + 10, 0, 0);
    self.attentionBtn.titleEdgeInsets = UIEdgeInsetsMake(0,-self.attentionBtn.frameWidth + 30, 0, 8);
    
    float leftPoin = self.acceptanceRateLabel2.frameLeft;
    [self.labelArray makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for(int i =0 ;i <dataArray.count;i++){
        NSString *title = dataArray[i];
        CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125] andMaxSize:CGSizeMake(320, UI_SCREEN_WITH *0.0625)];
        
        UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(leftPoin, self.acceptanceRateLabel2.frameBottom + 5, size.width + 5,UI_SCREEN_WITH *0.046875)];
        la.text = title;
        la.layer.masksToBounds =YES;
        la.layer.cornerRadius = 3.0;
        la.textAlignment =NSTextAlignmentCenter;
        la.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125];
        la.backgroundColor = UIColorFromRGB(0xe9e9e9);
        la.textColor = UIColorFromRGB(0x939393);
        [self addSubview:la];
        leftPoin =la.frameRight + 3;
        [self.labelArray addObject:la];
    }
    self.bottomLineImageView.frame = CGRectMake(0, self.frameHeight - 0.5, UI_SCREEN_WITH, 0.5);

}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

-(void)upCellInfoWith:(JMCollegesMode *)mode{
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:mode.logoString]];
    self.titleLable.text = mode.title;
    self.acceptanceRateLabel.text = mode.accept;
    NSString *str = [mode.levelString stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
    
    NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
    NSString * theString = [filteredArray componentsJoinedByString:@" "];
    
    NSMutableArray * array = (NSMutableArray *)[theString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"[]"]];
    for(int i = 0; i<array.count;i++){
        NSString *subStr =array[i];
        if(subStr.length == 0){
            [array removeObject:subStr];
        }
    }
    dataArray = array;
}
@end
